﻿
using ProjScan.Modules;
using ProjScan.ScanGUI.Tools;
using ProjScan.Tools;
using System;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    //
    // 摘要:
    //     错误资源页 - Class24
    public class FailAssetsWindow : EditorWindow
    {
        public enum jobType
        {
            fixingType,
            whiteListType
        }

        private string ruleName;

        private string ruleId;

        protected Rect windowRect = new Rect(20f, 20f, 500f, 600f);

        private GUIStyle infoBoxStyle;

        private bool supportWhiteList;

        private bool supportFailedData;

        private bool hasSrcPath;

        private bool supportFixing;

        private FileTreeView fileTreeView;

        [NonSerialized]
        private bool isFileTreeViewInitialized;

        [SerializeField]
        private TreeViewState treeViewState;

        [SerializeField]
        private MultiColumnHeaderState multiColumnHeaderState;

        private string pageInfo = "页面功能介绍:\n1. 点击此页面中显示的资源可在Editor目录中跳转至资源位。\n2. 使用靠右的勾选功能选择需要修复的资源。\n3. 此功能使用的修复方法均已开源，请在OpenSrcFixer\\文件夹中修改或添加资源的修改方式。";

        private Module moduleType;

        private void DrawWindow(int windowID)
        {
            GUI.BringWindowToFront(windowID);
        }

        public void DrawPageInfo()
        {
            GUILayout.BeginHorizontal();
            Label_BigBold(" 规则：" + ruleName);
            RefreshButton();
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.Label(pageInfo, infoBoxStyle);
            GUILayout.EndHorizontal();
            GUITools.DrawColoredRectangle(base.position.width);
        }

        public void DrawPageMain(float width)
        {
            if (hasSrcPath)
            {
                DrawRuleState.Initialize(2);
                EditorGUILayout.BeginVertical();
                GUITools.DrawBoxShadow(GetContentRect(), 1f);
                fileTreeView.OnGUI(GetContentRect());
                GUITools.EndVertical();
                if (DrawRuleState.EndRule(2))
                {
                    FailedResultManager.Instance.SaveFailResultInfo(ruleId);
                }
            }

            if (!supportFixing)
            {
                return;
            }

            GUITools.BeginStyledHorizontalLayout();
            GUIStyle buttonWithTextureSize = GetButtonWithTextureSize(16);
            if (supportFailedData && GUI.Button(GetButtonRect(), "开始修复勾选文件", buttonWithTextureSize))
            {
                if (!fileTreeView.IsResultEmpty())
                {
                    if (EditorUtility.DisplayDialog("修复资源", "该功能将修改您项目中的资源，请确认是否继续", "开始修复", "取消"))
                    {
                        ModulesManager.SingleFix(moduleType, ruleId);
                    }

                    fileTreeView.RefreshRows();
                }
                else
                {
                    EditorUtility.DisplayDialog("修复资源", "此规则中没有失败数据", "确认");
                }
            }

            GUITools.EndStyledHorizontalLayout();
        }

        private static GUIStyle GetButtonWithTextureSize(int size)
        {
            GUIStyle gUIStyle = new GUIStyle("button");
            gUIStyle.fontStyle = FontStyle.Bold;
            gUIStyle.fontSize = size;
            gUIStyle.normal.textColor = GUI.skin.label.normal.textColor;
            return gUIStyle;
        }

        public void init(Module moduleType, bool supportFailedData, bool supportFixing, bool supportWhiteList, bool hasSrcPath, string ruleName, string ruleID)
        {
            this.moduleType = moduleType;
            this.ruleName = ruleName;
            ruleId = ruleID;
            this.supportFailedData = supportFailedData;
            this.supportWhiteList = supportWhiteList;
            this.hasSrcPath = hasSrcPath;
            this.supportFixing = supportFixing;
            base.titleContent.text = "失败资源";
            setupGUIStyle();
            InitListTreeView();
        }

        private void InitListTreeView()
        {
            if (!isFileTreeViewInitialized)
            {
                if (treeViewState == null)
                {
                    treeViewState = new TreeViewState();
                }

                bool flag = multiColumnHeaderState == null;
                MultiColumnHeaderState destination = FileTreeView.CreateMultiColumnHeaderState(GetContentRect().width, supportFixing, supportWhiteList);
                if (MultiColumnHeaderState.CanOverwriteSerializedFields(multiColumnHeaderState, destination))
                {
                    MultiColumnHeaderState.OverwriteSerializedFields(multiColumnHeaderState, destination);
                }

                multiColumnHeaderState = destination;
                FailedDataColumnHeader failedDataColumnHeader = new FailedDataColumnHeader(multiColumnHeaderState, ruleId);
                if (flag)
                {
                    failedDataColumnHeader.ResizeToFit();
                }

                fileTreeView = new FileTreeView(treeViewState, failedDataColumnHeader, ruleId, ruleName, moduleType, supportFixing, supportWhiteList);
                isFileTreeViewInitialized = true;
            }

            fileTreeView.RefreshRows();
        }

        public static void Label_BigBold(string txt)
        {
            GUIStyle gUIStyle = new GUIStyle();
            gUIStyle.fontStyle = FontStyle.Bold;
            gUIStyle.fontSize = 16;
            gUIStyle.normal.textColor = GUI.skin.label.normal.textColor;
            GUILayout.Label(txt, gUIStyle);
        }

        private Rect GetContentRect()
        {
            return new Rect(5f, 135f, base.position.width - 20f, base.position.height - 250f);
        }

        public void OnGUI()
        {
            windowRect = GUI.Window(0, windowRect, DrawWindow, "Fixing Window");
            DrawPageInfo();
            DrawPageMain(windowRect.width);
        }

        private void OnSelectionChange()
        {
            fileTreeView.Reload();
        }

        private Rect GetButtonRect()
        {
            float num = 200f;
            float num2 = 30f;
            return new Rect((base.position.width - num) / 2f, (GetContentRect().y + GetContentRect().height + base.position.height - num2) / 2f, num, num2);
        }

        public void RefreshButton()
        {
            GUILayoutOption[] options = new GUILayoutOption[1]
            {
                GUILayout.Width(100f)
            };
            if (GUILayout.Button("刷新", options))
            {
                fileTreeView.RefreshRows();
            }
        }

        private void setupGUIStyle()
        {
            infoBoxStyle = Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.InfoBox);
        }
    }
}